package fun.symtable.tablemodel;

import fun.symtable.FunctionEntry;

public class FunctionTableModel extends SymbolTableModel<FunctionEntry> {
    private static String[] columnNames = { "Scope", "Name", "Parameters", "Expression" };

    public String getColumnName(int column) {
	return columnNames[column];
    }

    public int getColumnCount() {
	return columnNames.length;
    }

    public Object getValueAt(int row, int column) {
	if (0 == column)
	    return this.scope.get(row);
	FunctionEntry method = this.data.get(row);
	switch (column) {
	case 1:
	    return method.getName();
	case 2:
	    return arrayToString(method.getParameters());
	case 3:
	    return method.getExpression();
	}
	return "(error)";
    }

    private <T> String arrayToString(T[] array) {
	if (null == array || 0 == array.length)
	    return "";
	StringBuilder buffer = new StringBuilder();
	int i = 0;
	while (true) {
	    buffer.append(String.valueOf(array[i++]));
	    if (i == array.length)
		return buffer.toString();
	    buffer.append(", ");
	}
    }
}
